<template>
  <el-dialog
    :visible.sync="dialogVisible"
    width="800px"
    :before-close="handleClose"
    :close-on-click-modal="false"
    append-to-body
    :title="title"
  >
    <el-form
      :model="form"
      ref="formRef"
      label-width="140px"
      style="max-height: 580px; overflow: hidden; overflow-y: scroll"
    >
      <el-form-item
        prop="type"
        label="类型"
        :rules="[{ required: true, message: '请选择类型', trigger: 'blur' }]"
      >
        <el-select
          @change="$forceUpdate()"
          v-model="form.type"
          clearable
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in typeArr"
            :key="item.name"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        prop="type"
        label="药品所属"
        :rules="[{ required: true, message: '请选择类型', trigger: 'blur' }]"
      >
        <el-select
          @change="$forceUpdate()"
          v-model="form.DrugClassCode"
          clearable
          filterable
          placeholder="请选择"
        >
          <el-option
            v-for="item in DrugClassCodeArr"
            :key="item.name"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="药品图片" prop="pic">
        <!--  {{ imgsDefalut }} -->
        <ImgUploadPath
          ref="ImgUploadPath"
          :limit="1"
          @success="onUploadSuccess"
          @remove="onUploadRemove"
          :imgsDefalut="imgsDefalut"
        />
      </el-form-item>
      <!-- 
      <el-form-item prop="sub_department_id" label="科室"
        :rules="[{ required: true, message: '请选择机构名称', trigger: 'blur' }]">
        <el-select v-model="form.sub_department_id" clearable filterable placeholder="请选择">
          <el-option v-for="item in subDepartMentArr" :key="item.name" :label="item.name" :value="item.id">
          </el-option>
        </el-select>
      </el-form-item> -->

      <el-form-item
        label="药品名称"
        prop="name"
        :rules="[{ required: true, message: '请输入工号', trigger: 'blur' }]"
      >
        <el-input placeholder="请输入" v-model="form.name"></el-input>
      </el-form-item>

      <el-form-item
        label="通用名称"
        prop="common_name"
        :rules="[
          { required: true, message: '请输入通用名称', trigger: 'blur' },
        ]"
      >
        <el-input placeholder="请输入" v-model="form.common_name"></el-input>
      </el-form-item>

      <el-form-item
        label="药品单价"
        prop="price"
        :rules="[
          { required: true, message: '请输入药品单价', trigger: 'blur' },
        ]"
      >
        <el-input
          type="number"
          placeholder="请输入"
          v-model="form.price"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="药品介绍"
        prop="introduce"
        :rules="[
          { required: true, message: '请输入药品介绍', trigger: 'blur' },
        ]"
      >
        <el-input
          resize="none"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          placeholder="请输入"
          v-model="form.introduce"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="药品规格"
        prop="size"
        :rules="[
          { required: true, message: '请输入药品规格', trigger: 'blur' },
        ]"
      >
        <el-input placeholder="请输入" v-model="form.size"></el-input>
      </el-form-item>

      
      <el-form-item
        label="药品单位"
        prop="unit"
        :rules="[
          { required: true, message: '请输入药品单位', trigger: 'blur' },
        ]"
      >
        <el-input placeholder="请输入" v-model="form.unit"></el-input>
      </el-form-item>

      <el-form-item
        label="药品成分"
        prop="ingredient"
        :rules="[
          { required: true, message: '请输入药品成分', trigger: 'blur' },
        ]"
      >
        <el-input placeholder="请输入" v-model="form.ingredient"></el-input>
      </el-form-item>

      <el-form-item
        label="功能主治/适应症"
        prop="feature"
        :rules="[{ required: true, message: '请输入', trigger: 'blur' }]"
      >
        <el-input placeholder="请输入" v-model="form.feature"></el-input>
      </el-form-item>

      <el-form-item
        label="存储条件"
        prop="save"
        :rules="[
          { required: true, message: '请输入存储条件', trigger: 'blur' },
        ]"
      >
        <el-input placeholder="请输入" v-model="form.save"></el-input>
      </el-form-item>

      <el-form-item
        label="注意事项"
        prop="note"
        :rules="[
          { required: true, message: '请输入注意事项', trigger: 'blur' },
        ]"
      >
        <el-input
          placeholder="请输入"
          type="textarea"
          :autosize="{ minRows: 2, maxRows: 4 }"
          v-model="form.note"
        ></el-input>
      </el-form-item>

      <el-form-item
        label="用药指导"
        type="textarea"
        :autosize="{ minRows: 2, maxRows: 4 }"
        prop="guide"
        :rules="[
          { required: true, message: '请输入用药指导', trigger: 'blur' },
        ]"
      >
        <el-input placeholder="请输入" v-model="form.guide"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="tx-c">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="confirm" :loading="btnLoading"
        >确认</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { mapState, mapGetters } from "vuex";
import { debounce } from "@/util";
import { ApiMedicineEdit, ApiMedicineAdd } from "@/api/index.js";
import ImgUploadPath from "@/components/imgUpload-path.vue";
import { validatePhone } from "@/util/validate";
export default {
  props: ["roleData", "departmentData", "unitList", "subDepartMentArr"],
  components: {
    ImgUploadPath,
  },
  data() {
    return {
      validatePhone,
      //职称
      //医生职称
      docLevel: [
        { name: "副主任医师", code: "副主任医师" },
        { name: "主任医师", code: "主任医师" },
        { name: "主治医师", code: "主治医师" },
        { name: "医师", code: "医师" },
        { name: "医士", code: "医士" },
      ],
      DrugClassCodeArr: [
        { name: "中药", code: "中药" },
        { name: "西药", code: "西药" },
      ],
      //护士职称
      huLevel: [
        { name: "副主任护师", code: "副主任护师" },
        { name: "主任护师", code: "主任护师" },
        { name: "主管护师", code: "主管护师" },
        { name: "护师", code: "护师" },
        { name: "护士", code: "护士" },
      ],
      yaoLevel: [
        { name: "‌副主任药师", code: "‌副主任药师" },
        { name: "主任药师", code: "主任药师" },
        { name: "‌主管药师", code: "‌主管药师" },
        { name: "‌药师", code: "‌药师" },
        { name: "‌药士", code: "‌药士" },
      ],

      optionsData: [
        { name: "男", code: "男" },
        { name: "女", code: "女" },
      ],
      levelArr: [
        { name: "普通", code: "普通" },
        { name: "中级", code: "中级" },
        { name: "高级", code: "高级" },
      ],
      typeArr: [
        { name: "处方药", code: "处方药" },
        { name: "非处方药", code: "非处方药" },
      ],
      commentArr: [
        { name: "慢病药物方向", code: "慢病药物方向" },
        { name: "精神药物方向", code: "精神药物方向" },
        { name: "肿瘤药物方向", code: "肿瘤药物方向" },
      ],
      //接诊方式
      /* consultation_type */
      /*    consultation_typeArr: [
        { name: "全部", code: "0" },
        { name: "图文问诊", code: "1" },
        { name: "视频问诊", code: "2" },
      ], */

      oldform: {
        name: "",
        common_name: "",
        type: "",
        icon: "",
        introduce: "",
        price: "",
        size: "",
        ingredient: "",
        feature: "",
        save: "",
        note: "",
        guide: "",
      },
      form: {},
      btnLoading: false,
      dialogVisible: false,
      id: null,
      title: "",
      imgsDefalut: [],
      roleArr: [
        { name: "医师", code: "医师" },
        { name: "护师", code: "护师" },
      ],
    };
  },
  computed: {
    ...mapState(["userState"]),
  },
  methods: {
    /*   getUnitList() {
        ApigetUnitList({ page: 1, size: 999 }).then(res => {
          this.unitListArr = res.data
        })
      }, */
    onUploadSuccess(path) {
      this.form.pic = path;
    },
    onUploadRemove() {
      this.form.pic = "";
    },
    confirm: debounce(
      function () {
        this.$refs["formRef"].validate(async (valid) => {
          if (valid) {
            this.btnLoading = true;

            try {
              if (this.id) {
                await ApiMedicineEdit(this.form, this.id);
                this.$message.success("修改成功");
              } else {
                await ApiMedicineAdd(this.form);
                this.$message.success("添加成功");
                /* this.$alert("添加成功，该员工初始密码为：123456", "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {},
                }); */
              }

              this.handleClose();
              this.$emit("refresh");
            } finally {
              this.btnLoading = false;
            }
          } else return false;
        });
      },
      500,
      true
    ),

    show(row) {
      console.log(this.unitList);
      const data = row || this.oldform;
      for (const key in this.oldform) {
        this.$set(this.form, key, data[key]);
      }
      if (row) {
        this.id = row.id;
        this.title = "编辑药品";
        if (row.pic) this.imgsDefalut = [{ url: this.$path + row.pic }];
      } else {
        this.id = null;
        this.title = "添加药品";
      }
      this.dialogVisible = true;
    },

    handleClose() {
      this.dialogVisible = false;
      this.form = { ...this.oldform };
      this.$nextTick((_) => {
        this.$refs.formRef.clearValidate();
        this.$refs.ImgUploadPath.clear();
      });
    },
  },
};
</script>

<style></style>
